/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.comport;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;

public class ComDev implements SerialPortEventListener
{
	private String				Index			= "";
	private String				Port			= "";
	private int					Baudrate		= 9600;
	private int					Bytesize		= 8;
	private gnu.io.SerialPort	Serial			= null;
	private int					Timeout			= 2000;	// 毫秒
	private byte[]				ReturnValue		= null;
	private boolean				ReturnResult	= false;
	private boolean				isOpen			= false;
	private InputStream			inputStream		= null;
	private OutputStream		outputStream	= null;

	public ComDev(String inputPort, int inputBaudrate, int inputBytesize, int inputTimeout, String inputIndex)
	{
		this.setPort(inputPort);
		this.setBaudrate(inputBaudrate);
		this.setBytesize(inputBytesize);
		if(inputTimeout != 0)
			this.setTimeout(inputTimeout);
		this.setIndex(inputIndex);
		OpenSeriaPort();
	}

	public boolean CloseSeriaPort()
	{
		if(this.isOpen)
		{
			try
			{
				this.Serial.notifyOnDataAvailable(false);
				this.Serial.removeEventListener();
				isOpen = false;
				try
				{
					if(!java.util.Objects.equals(this.inputStream,null))
						this.inputStream.close();
					this.inputStream = null;
				}
				catch(Exception ee)
				{
				}
				try
				{
					if(!java.util.Objects.equals(this.outputStream,null))
						this.outputStream.close();
					this.outputStream = null;
				}
				catch(Exception ee)
				{
				}
				return true;
			}
			catch(Exception ex)
			{
				Com_Fun.debug_in(ex,this.toString());
				return false;
			}
		}else
		{
			return true;
		}
	}

	public boolean OpenSeriaPort()
	{
		try
		{
			if(java.util.Objects.equals(this.Serial,null))
			{
				// System.out.println("a1");
				CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(Port);
				// System.out.println("a2");
				this.Serial = (SerialPort) portId.open(this.Port,this.Timeout);
				// System.out.println("a3");
				// 判断是不是串口
				if(Serial instanceof SerialPort)
				{
					// System.out.println("a4");
					// 设置一下串口的波特率等参数
					this.Serial.setSerialPortParams(this.Baudrate,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
					// System.out.println("a5");
					this.Serial.addEventListener((SerialPortEventListener) this);
					// System.out.println("a6");
					this.isOpen = true;
					// System.out.println("the port success:" + this.Port + "|"
					// + this.Baudrate + "|" + this.Bytesize + "|" +
					// this.Index);
					return true;
				}else
				{
					System.out.println("ComDev.OpenSeriaPort:not com port");
					this.isOpen = false;
					return false;
				}
			}else
			{
				return this.isOpen;
			}
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString());
			return false;
		}
	}

	public synchronized void writeBuff(byte[] message)
	{
		try
		{
			if(this.isOpen)
			{
				this.ReturnValue = null;
				outputStream = new BufferedOutputStream(this.Serial.getOutputStream());
				try
				{
					this.ReturnResult = false;
					outputStream.write(message);
					outputStream.flush();
					ReadSeria();
					System.out.println("send com data success:"
							+ new String(message,Com_Para.U_CODE));
				}
				catch(IOException e)
				{
					Com_Fun.debug_in(e,this.toString());
				}
				finally
				{
					try
					{
						if(!java.util.Objects.equals(this.outputStream,null))
							this.outputStream.close();
					}
					catch(Exception e)
					{
					}
				}
			}else
			{
				OpenSeriaPort();
				if(this.isOpen)
				{
					outputStream = new BufferedOutputStream(this.Serial.getOutputStream());
					try
					{
						this.ReturnValue = null;
						this.ReturnResult = false;
						outputStream.write(message);
						outputStream.flush();
						ReadSeria();
						System.out.println("send com data success:"
								+ new String(message,Com_Para.U_CODE));
					}
					catch(IOException e)
					{
						Com_Fun.debug_in(e,this.toString());
					}
					finally
					{
						try
						{
							if(!java.util.Objects.equals(this.outputStream,null))
								this.outputStream.close();
						}
						catch(Exception e)
						{
						}
					}
				}
			}
		}
		catch(IOException e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
	}

	public synchronized int ReadSeria()
	{
		int iResult = -1;
		try
		{
			InputStream inputStream = this.Serial.getInputStream();
			while(this.isOpen && inputStream.available() > 0)
			{
				this.ReturnValue = new byte[1024];
				iResult = inputStream.read(this.ReturnValue);
				this.ReturnResult = true;
				Thread.sleep(100);
			}
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
		return iResult;
	}

	public synchronized void serialEvent(SerialPortEvent seEvent)
	{
		System.out.println("get serialEvent");
		// switch(seEvent.getEventType())
		// {
		// case SerialPortEvent.BI:
		// case SerialPortEvent.OE:
		// case SerialPortEvent.FE:
		// case SerialPortEvent.PE:
		// case SerialPortEvent.CD:
		// case SerialPortEvent.CTS:
		// case SerialPortEvent.DSR:
		// case SerialPortEvent.RI:
		// case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
		// break;
		// case SerialPortEvent.DATA_AVAILABLE:
		// try
		// {
		// InputStream inputStream = this.Serial.getInputStream();
		// this.ReturnValue = new byte[1024];
		// while(this.isOpen && inputStream.available() > 0)
		// {
		// int numBytes = inputStream.read(this.ReturnValue);
		// this.ReturnResult = true;
		// Thread.sleep(100);
		// }
		// }
		// catch(Exception e)
		// {
		// System.out.println(e.getMessage());
		// }
		// break;
		// }
	}

	public String getIndex()
	{
		return Index;
	}

	public void setIndex(String index)
	{
		Index = index;
	}

	public String getPort()
	{
		return Port;
	}

	public void setPort(String port)
	{
		Port = port;
	}

	public int getBaudrate()
	{
		return Baudrate;
	}

	public void setBaudrate(int baudrate)
	{
		Baudrate = baudrate;
	}

	public int getBytesize()
	{
		return Bytesize;
	}

	public void setBytesize(int bytesize)
	{
		Bytesize = bytesize;
	}

	public Object getSerial()
	{
		return Serial;
	}

	public void setSerial(SerialPort serial)
	{
		Serial = serial;
	}

	public double getTimeout()
	{
		return Timeout;
	}

	public void setTimeout(int timeout)
	{
		Timeout = timeout;
	}

	public byte[] getReturnValue()
	{
		return ReturnValue;
	}

	public boolean isOpen()
	{
		return isOpen;
	}

	public void setOpen(boolean isOpen)
	{
		this.isOpen = isOpen;
	}

	public void setReturnValue(byte[] returnValue)
	{
		ReturnValue = returnValue;
	}

	public boolean isReturnResult()
	{
		return ReturnResult;
	}

	public void setReturnResult(boolean returnResult)
	{
		ReturnResult = returnResult;
	}
}
